home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / binutils.252 / ld / ldctor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-15  |  5.1 KB  |  185 lines

  1. /* ldctor.c -- constructor support routines
  2.    Copyright (C) 1991, 92, 93, 94 Free Software Foundation, Inc.
  3.    By Steve Chamberlain <sac@cygnus.com>
  4.    
  5. This file is part of GLD, the Gnu Linker.
  6.  
  7. GLD is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GLD is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GLD; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "bfd.h"
  22. #include "sysdep.h"
  23. #include "bfdlink.h"
  24.  
  25. #include "ld.h"
  26. #include "ldexp.h"
  27. #include "ldlang.h"
  28. #include "ldmisc.h"
  29. #include "ldgram.h"
  30. #include "ldmain.h"
  31. #include "ldctor.h"
  32.  
  33. /* The list of statements needed to handle constructors.  These are
  34.    invoked by the command CONSTRUCTORS in the linker script.  */
  35. lang_statement_list_type constructor_list;
  36.  
  37. /* We keep a list of these structures for each sets we build.  */
  38.  
  39. struct set_info
  40. {
  41.   struct set_info *next;        /* Next set.  */
  42.   struct bfd_link_hash_entry *h;    /* Hash table entry.  */
  43.   bfd_reloc_code_real_type reloc;    /* Reloc to use for an entry.  */
  44.   size_t count;                /* Number of elements.  */
  45.   struct set_element *elements;        /* Elements in set.  */
  46. };
  47.  
  48. struct set_element
  49. {
  50.   struct set_element *next;        /* Next element.  */
  51.   asection *section;            /* Section of value.  */
  52.   bfd_vma value;            /* Value.  */
  53. };
  54.  
  55. /* The sets we have seen.  */
  56.  
  57. static struct set_info *sets;
  58.  
  59. /* Add an entry to a set.  H is the entry in the linker hash table.
  60.    RELOC is the relocation to use for an entry in the set.  SECTION
  61.    and VALUE are the value to add.  This is called during the first
  62.    phase of the link, when we are still gathering symbols together.
  63.    We just record the information now.  The ldctor_find_constructors
  64.    function will construct the sets.  */
  65.  
  66. void
  67. ldctor_add_set_entry (h, reloc, section, value)
  68.      struct bfd_link_hash_entry *h;
  69.      bfd_reloc_code_real_type reloc;
  70.      asection *section;
  71.      bfd_vma value;
  72. {
  73.   struct set_info *p;
  74.   struct set_element *e;
  75.   struct set_element **epp;
  76.  
  77.   for (p = sets; p != (struct set_info *) NULL; p = p->next)
  78.     if (p->h == h)
  79.       break;
  80.  
  81.   if (p == (struct set_info *) NULL)
  82.     {
  83.       p = (struct set_info *) xmalloc (sizeof (struct set_info));
  84.       p->next = sets;
  85.       sets = p;
  86.       p->h = h;
  87.       p->reloc = reloc;
  88.       p->count = 0;
  89.       p->elements = NULL;
  90.     }
  91.   else if (p->reloc != reloc)
  92.     einfo ("%P%X: Different relocs used in set %s\n", h->root.string);
  93.  
  94.   e = (struct set_element *) xmalloc (sizeof (struct set_element));
  95.   e->next = NULL;
  96.   e->section = section;
  97.   e->value = value;
  98.  
  99.   for (epp = &p->elements; *epp != NULL; epp = &(*epp)->next)
  100.     ;
  101.   *epp = e;
  102.  
  103.   ++p->count;
  104. }
  105.  
  106. /* This function is called after the first phase of the link and
  107.    before the second phase.  At this point all set information has
  108.    been gathered.  We now put the statements to build the sets
  109.    themselves into constructor_list.  */
  110.  
  111. void
  112. ldctor_build_sets ()
  113. {
  114.   lang_statement_list_type *old;
  115.   struct set_info *p;
  116.  
  117.   old = stat_ptr;
  118.   stat_ptr = &constructor_list;
  119.  
  120.   lang_list_init (stat_ptr);
  121.  
  122.   for (p = sets; p != (struct set_info *) NULL; p = p->next)
  123.     {
  124.       struct set_element *e;
  125.       const reloc_howto_type *howto;
  126.       int size;
  127.  
  128.       /* If the symbol is defined, we may have been invoked from
  129.      collect, and the sets may already have been built, so we do
  130.      not do anything.  */
  131.       if (p->h->type == bfd_link_hash_defined)
  132.     continue;
  133.  
  134.       /* For each set we build:
  135.        set:
  136.          .long number_of_elements
  137.          .long element0
  138.          ...
  139.          .long elementN
  140.          .long 0
  141.      except that we use the right size instead of .long.  When
  142.      generating relocateable output, we generate relocs instead of
  143.      addresses.  */
  144.       howto = bfd_reloc_type_lookup (output_bfd, p->reloc);
  145.       if (howto == (reloc_howto_type *) NULL)
  146.     {
  147.       einfo ("%P%X: BFD backend does not support reloc %d for set %s\n",
  148.          (int) p->reloc, p->h->root.string);
  149.       size = LONG;
  150.     }
  151.       else
  152.     {
  153.       switch (bfd_get_reloc_size (howto))
  154.         {
  155.         case 1: size = BYTE; break;
  156.         case 2: size = SHORT; break;
  157.         case 4: size = LONG; break;
  158.         case 8: size = QUAD; break;
  159.         default:
  160.           einfo ("%P%X: Unsupported size %d for set %s\n",
  161.              bfd_get_reloc_size (howto), p->h->root.string);
  162.           size = LONG;
  163.           break;
  164.         }
  165.     }
  166.  
  167.       lang_add_assignment (exp_assop ('=', p->h->root.string,
  168.                       exp_nameop (NAME, ".")));
  169.       lang_add_data (size, exp_intop ((bfd_vma) p->count));
  170.  
  171.       for (e = p->elements; e != (struct set_element *) NULL; e = e->next)
  172.     {
  173.       if (link_info.relocateable)
  174.         lang_add_reloc (p->reloc, howto, e->section,
  175.                 (const char *) NULL, exp_intop (e->value));
  176.       else
  177.         lang_add_data (size, exp_relop (e->section, e->value));
  178.     }
  179.  
  180.       lang_add_data (size, exp_intop (0));
  181.     }
  182.  
  183.   stat_ptr = old;
  184. }
  185.